#include "playbackbottom.h"
#include <QHBoxLayout>
#include <QDebug>

PlaybackBottom::PlaybackBottom(QWidget* p)
    :QWidget(p)
{
    //禁止父窗口影响子窗口样式
    setAttribute(Qt::WA_StyledBackground);
    setMouseTracking(true);

    this->setFixedHeight(80);

    setStyleSheet("background-color:rgb(67,67,67)");

    QString label_qss = "QLabel{font-family:Microsoft YaHei; font-size:18px; color:rgb(255,255,255);}";

    CurPlayTimeLabel = new QLabel(this);
    CurPlayTimeLabel->setText("00:00:00");
    CurPlayTimeLabel->setAlignment(Qt::AlignRight);
    CurPlayTimeLabel->setFixedSize(80, 32);
    CurPlayTimeLabel->setStyleSheet(label_qss);

    TotalTimeLabel = new QLabel(this);
    TotalTimeLabel->setText("00:00:00");
    TotalTimeLabel->setAlignment(Qt::AlignLeft);
    TotalTimeLabel->setFixedSize(160, 32);
    TotalTimeLabel->setStyleSheet(label_qss);

    PlayButtonGroupWidget = new PlaybackPlayButtonGroup(this);

    EffectButton = new QPushButton(this);
    EffectButton->setText("");
    EffectButton->setFixedSize(32, 32);
    EffectButton->setStyleSheet("QPushButton{background-image:url(:/playCtrlBar/resources/playctrlBar/effect.svg);border:none;}"
        "QPushButton:hover{background-image:url(:/playCtrlBar/resources/playctrlBar/effect_hover.svg);border:none;}"
        "QPushButton:pressed{background-image:url(:/playCtrlBar/resources/playctrlBar/effect.svg);border:none;}");

//    FullscreenButton = new QPushButton(this);
//    FullscreenButton->setText("");
//    FullscreenButton->setFixedSize(32, 32);
//    FullscreenButton->setStyleSheet("QPushButton{background-image:url(:/playCtrlBar/resources/playctrlBar/fullscreen.svg);border:none;}"
//        "QPushButton:hover{background-image:url(:/playCtrlBar/resources/playctrlBar/fullscreen_hover.svg);border:none;}"
//        "QPushButton:pressed{background-image:url(:/playCtrlBar/resources/playctrlBar/fullscreen.svg);border:none;}");

    QHBoxLayout* pHLay = new QHBoxLayout(this);
    pHLay->addSpacing(3);
    pHLay->addWidget(CurPlayTimeLabel);
    pHLay->addSpacing(1);
    pHLay->addWidget(TotalTimeLabel);
    pHLay->addStretch();
    pHLay->addWidget(PlayButtonGroupWidget);
    pHLay->addStretch();
    pHLay->addWidget(EffectButton);
    pHLay->addSpacing(18);
    //pHLay->addWidget(FullscreenButton);

    showTimeLabel(false);

    connect(PlayButtonGroupWidget, &PlaybackPlayButtonGroup::sig_play, this, &PlaybackBottom::sig_play);
    connect(PlayButtonGroupWidget, &PlaybackPlayButtonGroup::sig_stop, this, &PlaybackBottom::sig_stop);
    //connect(FullscreenButton, &QPushButton::clicked, this, &PlaybackBottom::sig_fullScreen);

    connect(EffectButton, &QPushButton::clicked, this, &PlaybackBottom::sig_list);
}

PlaybackBottom::~PlaybackBottom()
{

}

void PlaybackBottom::showTimeLabel(bool isShow)
{
    if (isShow)
    {
        CurPlayTimeLabel->show();
        TotalTimeLabel->show();
    }
    else
    {
        CurPlayTimeLabel->hide();
        TotalTimeLabel->hide();
    }
}

QString PlaybackBottom::formatTimeMs(const qint64& timeMs)
{
    qint64 seconds = timeMs;
    int hours = seconds / 3600;
    int mins = (seconds - hours * 3600) / 60;
    int secs = seconds - hours * 3600 - mins * 60;

    char buf[1024] = { 0 };
    sprintf(buf, "%02d:%02d:%02d", hours, mins, secs);

    return QString(buf);
}

//设置当前播放时间
void PlaybackBottom::setCurPlayTime(const qint64& curMs)
{
    CurPlayTimeLabel->setText(formatTimeMs(curMs));
}

//设置总的播放时间
void PlaybackBottom::setTotalTime(const qint64& totalMs)
{
    TotalTimeLabel->setText("/" + formatTimeMs(totalMs));
}

void PlaybackBottom::resizeEvent(QResizeEvent* event)
{
    (void)event;
    if (PlayButtonGroupWidget)
    {
        int x = this->width() / 2 - PlayButtonGroupWidget->width() / 2;
        int y = this->height() / 2 - PlayButtonGroupWidget->height() / 2;
        PlayButtonGroupWidget->move(x, y);
    }
}
